﻿using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Tapper
{
    class Client : Character
    {
        //Fazer
        //- Limite de tela no cliente.
        //- Ao puxar player no GameOVer ao passar pela porta.Apagar Entidade de ambos
        //- Criar temporizador por parametro do Cliente de cada mesa.
        //- Criar distancia a ser andada para tras. se a distancia for maior que o limite da Porta.O cliente vai embora

        public Table m_table;
        public float m_timer; // tempo em segundos        
        internal readonly float m_distanceOnMug;
        internal Mugs m_mugs;        
        private bool m_isMoving;
        public Vector2 m_moveDir;
        internal bool m_getsPlayer;        
        private readonly float m_timerStay;
        private float m_timerDistance; // tempo em segundos
        public readonly TableState m_tableState;
        private float mposX;
        public Vector2 m_originalPos;
        
        public Client(World world, Texture2D texture, Vector2 pos, Vector2 vel, float distanceOnMug, Mugs mugs, TableState tableState)
            : base(world, texture, pos, vel)
        {
            m_distanceOnMug = distanceOnMug;
            m_mugs          = mugs;
            m_accel         = 25.0f;
            m_maxvel        = 100.0f;
            m_vel           = vel;
            m_originalPos   = m_pos;
            m_pos.X         = pos.X;
            mposX           = pos.X;
            m_tableState    = tableState;
            m_isMoving      = true;
            m_timerDistance = 0.5f;
            m_moveDir       = new Vector2(0.0f, 0.0f);

            //! GamePlay Timer do tempo parado do client, no seu estado de movimentação
            switch (m_tableState)
            {
                case TableState.OnTableOne:
                    m_timerStay = 6.43f;
                    //m_timerStay = 3.5f;
                    break;
                case TableState.OnTableTwo:
                    m_timerStay = 5.32f;
                   break;
                case TableState.OnTableTree:
                   m_timerStay = 2.11f;
                   break;
                case TableState.OnTableFour:
                   m_timerStay = 1.13f;
                   break;
            }
        }

        public override float LayerTexture()
        {
            return 0.2f;
        }

        public override void Update(GameTime gametime)
        {
            switch (m_world.m_gameState)
            {
                case GameState.OnGame:
                    m_timer -= (float)gametime.ElapsedGameTime.TotalSeconds;
                    if (m_timer <= 0.0f)
                    {
                        if (m_isMoving)
                        {                            
                            m_moveDir.X = -1;
                            m_timer     = m_timerDistance; //segundos movimentando
                        }
                        else
                        {
                            m_moveDir.X = 0;
                            m_timer = m_timerStay; //segundos parado 
                        }
                        m_isMoving = !m_isMoving;
                    }
                    break;
                case GameState.OnGameOver:
                    m_moveDir.X = 0;
                    if (m_getsPlayer)
                    {
                        //Todo cria timer para o player ser pego
                        foreach (var player in m_world.m_entities.Select(current => current as Player).Where(player => player != null))
                        {
                            m_moveDir.X  = 1;                            
                            player.m_pos = m_pos;
                            player.m_tableState = m_tableState;
                        }
                    }
                    break;
            }
            m_moveDir.Y = 0;
            m_pos += m_moveDir * m_vel * (float)gametime.ElapsedGameTime.TotalSeconds;
        }        

        public void Reset()
        {            
            m_mugs          = null;
            m_pos.X         = mposX;
            m_isMoving      = true;
            m_timerDistance = 0.5f;
            m_moveDir       = new Vector2(0.0f, 0.0f);
        }
    }
}
